package com.nan.hrm.service.impl;

import com.nan.hrm.CourseEsClient;
import com.nan.hrm.doc.CourseDoc;
import com.nan.hrm.domain.Course;
import com.nan.hrm.domain.CourseDetail;
import com.nan.hrm.domain.CourseMarket;
import com.nan.hrm.dto.CourseAddDto;
import com.nan.hrm.mapper.CourseDetailMapper;
import com.nan.hrm.mapper.CourseMapper;
import com.nan.hrm.mapper.CourseMarketMapper;
import com.nan.hrm.service.ICourseService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.nan.hrm.util.AjaxResult;
import com.nan.hrm.util.ValidUtils;
import com.nan.hrm.util.exception.GlobalException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.nan.hrm.config.RabbitMQConfig.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author N
 * @since 2021-09-14
 */
@Service
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseDetailMapper courseDetailMapper;
    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private CourseEsClient courseEsClient;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private void publishMessage(CourseDoc courseDoc){
        String message = "你好,新的课程"+courseDoc.getName()+"已发布";
        rabbitTemplate.convertAndSend(EXCHANGE_NAME_TOPIC,"new.email",message);
        rabbitTemplate.convertAndSend(EXCHANGE_NAME_TOPIC,"new.sms",message);
        rabbitTemplate.convertAndSend(EXCHANGE_NAME_TOPIC,"new.system",message);
    }
    /**
     * 添加课程
     * @param courseAddDto 课程临时对象
     * @return
     */
    @Override
    public AjaxResult save(CourseAddDto courseAddDto) {
        //数据校验
        Course course = courseAddDto.getCourse();
        CourseDetail courseDetail = courseAddDto.getCourseDetail();
        CourseMarket courseMarket = courseAddDto.getCourseMarket();
        ValidUtils.assertNotNull(course.getName(),"课程名称不能为空!");
        //优先保存course数据
        course.setStatus(0);
        courseMapper.insert(course);
        //拿到id
        courseDetail.setId(course.getId());
        courseMarket.setId(course.getId());
        //保存数据
        courseDetailMapper.insert(courseDetail);
        courseMarketMapper.insert(courseMarket);

        return AjaxResult.me();
    }

    /**
     * 课程上线
     * @param id 课程上线id
     * @return
     */
    @Override
    public AjaxResult onLineCourse(Long id) {
        try {
            // 判断参数合法性
            ValidUtils.assertNotNull(id, "id无效");
            Course course = courseMapper.selectById(id);
            ValidUtils.assertNotNull(course, "无效选中项");
            // 获取课程的上下架参数,如果是1，说明已经上线
            Integer status = course.getStatus();
            if (status == 1) {
                throw new GlobalException("课程已经上线");
            }
            // 更新课程为上架
            course.setStatus(1);
            courseMapper.updateById(course);
            // 添加到ElasticSearch
            // 封装CourseDoc对象
            CourseMarket courseMarket = courseMarketMapper.selectById(id);
            CourseDoc courseDoc = new CourseDoc();
            BeanUtils.copyProperties(courseMarket, courseDoc);
            BeanUtils.copyProperties(course, courseDoc);
            //打印
            System.out.println(courseDoc);
            courseEsClient.save(courseDoc);

            //发布消息
            publishMessage(courseDoc);
        }catch(Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

        return AjaxResult.me();
    }

    @Override
    public AjaxResult offLineCourse(Long id) {
        try {
            // 判断参数合法性
            ValidUtils.assertNotNull(id, "id无效");
            Course course = courseMapper.selectById(id);
            ValidUtils.assertNotNull(course, "无效选中项");
            // 获取课程的上下架参数,如果是1，说明已经上线
            Integer status = course.getStatus();
            if (status == 0) {
                throw new GlobalException("课程已经下线");
            }
            // 更新课程为上架
            course.setStatus(0);
            courseMapper.updateById(course);
            // 删除ElasticSearch
            courseEsClient.delete(id);
        }catch(Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

        return AjaxResult.me();
    }
}
